<script setup>
import { ref, onMounted } from "vue";
import { queryAllApi, addApi,queryByIdApi,updateApi,deleteApi } from '@/api/dept';
import { ElMessage,ElMessageBox  } from 'element-plus'

const search = async () => {
  const res = await queryAllApi();
  if (res.code) {
    deptList.value = res.data;
  }
}

const deptList = ref([])

onMounted(() => {
  search();
})

const dialogFormVisible = ref(false);
const formTitle = ref('');
const dept = ref({ name: '' })

// 新增部门
const add = () => {
  if(deptFormRef.value){
    deptFormRef.value.resetFields();
  }
  dialogFormVisible.value = true;
  formTitle.value = '新增部门';
  dept.value = { name: '' }
}

//保存部门
const save = async () => {
  if (!deptFormRef) return
  await deptFormRef.value.validate(async (valid, fields) => {
    if (valid) {//通过
      let result;
      if(dept.value.id){
        result = await updateApi(dept.value);
      }else{
        result = await addApi(dept.value);
      }  
      if (result.code) {
        ElMessage.success('保存成功');
        dialogFormVisible.value = false;
        search();
      } else {
        ElMessage.error(result.msg);
      }
    } else {//不通过
      ElMessage.error('表单效验不通过,请检查表单');
    }
  });
}
//定义表单校验规则
const deptFormRef = ref();
const rules = ref({
  name: [
    { required: true, message: '部门名称为必填项', trigger: 'blur' },
    { min: 2, max: 10, message: '部门名称必须得在2-10位之间', trigger: 'blur' }
  ]
})

const selectById = async (id) => {
   const res = await queryByIdApi(id);
   if(res.code){
     dialogFormVisible.value = true;
     dept.value = res.data;
     formTitle.value = '修改部门';
   }
}

const deleteById = (id) => {
  ElMessageBox.confirm(
    '你确定要删除该部门吗?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then( async() => {
     const result = await deleteApi(id);
     if(result.code){
      ElMessage.success('删除成功');
      search();
     }else{
      ElMessage.error(result.msg);
     }
    })
    .catch(() => {
      ElMessage.info('已取消删除')
    })
}




</script>

<template>
  <h1>部门管理</h1>
  <div id="set">
    <el-button type="primary" @click="add">+ 新增部门</el-button>
  </div>

  <el-table :data="deptList" border style="width: 100%">
    <el-table-column type="index" label="序号" width="200" align="center" />
    <el-table-column prop="name" label="部门名称" width="300" align="center" />
    <el-table-column prop="updateTime" label="最后操作时间" width="300" align="center" />
    <el-table-column prop="address" label="操作" align="center">
      <template #default="scope">
        <el-button type="primary" size="small" @click="selectById(scope.row.id)">编辑</el-button>
        <el-button type="danger" size="small" @click="deleteById(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-dialog v-model="dialogFormVisible" :title="formTitle" width="500">
    <el-form :model="dept" :rules="rules" ref="deptFormRef">
      <el-form-item label="部门名称" label-width="100px" prop="name">
        <el-input v-model="dept.name" placeholder="请输入部门名称,长度2-10位" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>
#set {
  margin: 20px 0px;
}
</style>
